﻿package com.wandian.view.mediator
{

	import com.wandian.controller.CLZNoticeName;
	import com.wandian.controller.HelpCommand;
	import com.wandian.data.GameData;
	import com.wandian.model.SwfDataProxy;
	import com.wandian.util.CLZTabBar;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class HelpMediator extends Mediator
	{
		private var _swfData:SwfDataProxy;
		private var _nShowType:int;
		private var _mcHelp:MovieClip;
		private var _mcHelpTip:MovieClip;
		private var _global:GameData;
		private var _mcNewHelp:MovieClip;
		public static const NAME:String="HelpMediator";
		public static const TYPE_0:int=0;
		public static const TYPE_1:int=1;
		public static const TYPE_2:int=2;
		public static const TYPE_3:int=3;
		public static const TYPE_4:int=4;
		public static const TYPE_5:int=5;
		public static const TYPE_6:int=6;
		public static const TYPE_7:int=7;
		public static const TYPE_8:int=8;

		public function HelpMediator(ShowType:int=0, viewComponent:Object=null)
		{
			this._global=GameData.getInstance();
			this._nShowType=ShowType;
			super(NAME, viewComponent);
			return;
		} 

		override public function onRegister():void
		{
//			--this._swfData=facade.retrieveProxy(SwfDataProxy.NAME) as SwfDataProxy;
//			--this._swfData.singleLoadSWF("help", CLZNoticeName.HELPMEDIATOR_SHOW_HELP_LOADER_COMPLETE);//换成本地一次性加载
			this.sendNotification(CLZNoticeName.HELPMEDIATOR_SHOW_HELP_LOADER_COMPLETE);
			return;
		} 

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.HELPMEDIATOR_SHOW_HELP_LOADER_COMPLETE, CLZNoticeName.HELPMEDIATOR_HIDE_HELP, CLZNoticeName.SHOW_NEW_HELP, CLZNoticeName.HIDE_NEW_HELP];
		} 

		override public function handleNotification(param1:INotification):void
		{
			var tabs:Array=null;
			var tabBar:CLZTabBar=null;
			var count:int=0;
			switch (param1.getName())
			{
				case CLZNoticeName.HELPMEDIATOR_SHOW_HELP_LOADER_COMPLETE://帮助加载完毕
				{
					if (this._nShowType == 0)
					{
						if (this._mcHelpTip != null)
						{
							this.onHelpTipCloseHandler();
						}
						this._mcHelpTip=GameData.getMovieClip("HelpNewbie");
						Texas.I._mcWindosView.addChildTo(this._mcHelpTip);
						Texas.I._mcWindosView.centerXY(this._mcHelpTip, 0, 100);
						this._mcHelpTip.close_btn.addEventListener(MouseEvent.CLICK, this.onHelpTipCloseHandler);
						this._mcHelpTip.help_btn.addEventListener(MouseEvent.CLICK, this.onNewBtnHandler);
						this._mcHelpTip.startGame_btn.addEventListener(MouseEvent.CLICK, this.onStartGameHandler);
					}
					else
					{
						if (this._mcHelp != null)
						{
							this.onHelpCloseHandler(null);
						}
						this._mcHelp=GameData.getMovieClip("Help");
						Texas.I._mcWindosView.addChildTo(this._mcHelp);
						Texas.I._mcWindosView.centerXY(this._mcHelp);
						this._mcHelp.close_btn.addEventListener(MouseEvent.CLICK, this.onHelpCloseHandler);
						tabs=[[this._mcHelp.btn1], [this._mcHelp.btn2], [this._mcHelp.btn3], [this._mcHelp.btn4], [this._mcHelp.btn5], [this._mcHelp.btn6], [this._mcHelp.btn7], [this._mcHelp.btn8]];
						tabBar=new CLZTabBar(tabs);
						count=0;
						while (count < tabs.length)
						{

							tabs[count][0].addEventListener(Event.CHANGE, this.changeTabBarHandler);
							count=count + 1;
						}
						tabBar.setClickIndex(tabs[(this._nShowType - 1)][0]);
					}
					break;
				}
				case CLZNoticeName.HELPMEDIATOR_HIDE_HELP:
				{
					Texas.I._mcWindosView.removeChildTo(this._mcHelp);
					this._mcHelp=null;
					this._nShowType=0;
					facade.removeMediator(HelpMediator.NAME);
					break;
				}
				case CLZNoticeName.SHOW_NEW_HELP:
				{
					if (this._mcNewHelp != null)
					{
						this.sendNotification(CLZNoticeName.HIDE_NEW_HELP);
					}
					else
					{
						facade.registerCommand(CLZNoticeName.COMPLETE_NEW_HELP_REQ, HelpCommand);
						this._mcNewHelp=GameData.getMovieClip("TutorialMc");
						this._mcNewHelp.addEventListener(Event.CLOSE, this.onNewHelpCloseHandler);
						this._mcNewHelp.addEventListener(Event.COMPLETE, this.onNewHelpCloseStartGameHandler);
						Texas.I._mcWindosView.addChildTo(this._mcNewHelp);
						Texas.I._mcWindosView.centerXY(this._mcNewHelp, 0, 0);
					}
					break;
				}
				case CLZNoticeName.HIDE_NEW_HELP:
				{
					facade.removeCommand(CLZNoticeName.COMPLETE_NEW_HELP_REQ);
					this._mcNewHelp.removeEventListener(Event.CLOSE, this.onNewHelpCloseHandler);
					this._mcNewHelp.removeEventListener(Event.COMPLETE, this.onNewHelpCloseStartGameHandler);
					Texas.I._mcWindosView.removeChildTo(this._mcNewHelp);
					this._mcNewHelp=null;
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		} 

		private function onNewHelpCloseHandler(event:Event):void
		{
			this.sendNotification(CLZNoticeName.HIDE_NEW_HELP);
			this.onHelpCloseHandler();
			return;
		} 

		private function onNewHelpCloseStartGameHandler(event:Event=null):void
		{
			this.sendNotification(CLZNoticeName.COMPLETE_NEW_HELP_REQ);
			this.sendNotification(CLZNoticeName.HIDE_NEW_HELP);
			this.onStartGameHandler();
			this.onHelpCloseHandler();
			return;
		} 

		private function onStartGameHandler(event:MouseEvent=null):void
		{
			this.onHelpTipCloseHandler(1);
			if (int(this._global.userInfo.userVo.icon) == 0)
			{
				this.sendNotification(CLZNoticeName.SHOW_SELECTED_PERSON);
			}
			else
			{
				this.sendNotification(CLZNoticeName.FAST_ENTER_GAME_REQ);
			}
			this.sendNotification(CLZNoticeName.FAST_ENTER_GAME_REQ);
			return;
		} 

		private function onHelpTipCloseHandler(helpType:int=0):void
		{
			Texas.I._mcWindosView.removeChildTo(this._mcHelpTip);
			this._mcHelpTip=null;
			this._nShowType=0;
			if (helpType != 0)
			{
				facade.removeMediator(HelpMediator.NAME);
				this.onHelpCloseHandler();
			}
			return;
		} 

		private function onNewBtnHandler(event:MouseEvent):void
		{
//			this._swfData=facade.retrieveProxy(SwfDataProxy.NAME) as SwfDataProxy;
//			this._swfData.singleLoadSWF("newHelp", CLZNoticeName.SHOW_NEW_HELP);
			this.onHelpTipCloseHandler();
			this.facade.sendNotification(CLZNoticeName.SHOW_NEW_HELP);
			return;
		} 

		private function changeTabBarHandler(event:Event):void
		{
			var tab:*=event.target as MovieClip;
			var frame:*=int(tab.name.substr(-1));
			this._mcHelp.gotoAndStop(frame);
			return;
		} 

		private function onHelpCloseHandler(helpType:*=null):void
		{
			this.sendNotification(CLZNoticeName.HELPMEDIATOR_HIDE_HELP);
			return;
		} 

	}
}
